package pe.com.rimac.portal.facturacion.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import pe.com.rimac.portal.common.holder.RequestHolder;

import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.ibm.portal.um.PumaProfile;
import com.ibm.portal.um.portletservice.PumaHome;

/**
 * Clase utilitaria para la comunicaci&oacute;n con el porlet service PUMA.
 * 
 * @author Andres Deza Villacorta - Synopsis S.A
 * 
 */
public class PumaUtil
{
	@Autowired
	private PortletServiceHome pumaServiceHome;

	/**
	 * @param attribute
	 * @return
	 */
	public String getAttributeValue(PumaAttribute attribute)
	{
		List<String> attributes = new ArrayList<String>();
		attributes.add(attribute.getValue());
		PumaProfile profile = getPumaProfile();
		try
		{
			Map<String, Object> userAttribute = profile.getAttributes(profile.getCurrentUser(), attributes);
			return (String) userAttribute.get(attribute.getValue());
		}
		catch (Exception e)
		{
			throw new RuntimeException("No se pudo obtener los atributos del usuaroi", e);
		}
	}

	/**
	 * @return
	 */
	private PumaHome getPumaService()
	{
		try
		{
			return (PumaHome) pumaServiceHome.getPortletService(PumaHome.class);
		}
		catch (PortletServiceUnavailableException e)
		{
			throw new RuntimeException("No se pudo obtener el servicio Puma del Home", e);
		}
	}

	/**
	 * @return
	 */
	private PumaProfile getPumaProfile()
	{
		return getPumaService().getProfile(RequestHolder.getPortletRequest());
	}

	/**
	 * Clase que representa lo atributos disponibles a ser consultados a trav&eacute;s del servicio PUMA.
	 * 
	 * @author Andres Deza Villacorta - Synopsis S.A
	 * 
	 */
	public static class PumaAttribute
	{
		/**
		 * Common name
		 */
		public static final PumaAttribute CN = new PumaAttribute("cn");
		/**
		 * Surname
		 */
		public static final PumaAttribute SN = new PumaAttribute("sn");
		/**
		 * Uid
		 */
		public static final PumaAttribute UID = new PumaAttribute("uid");
		/**
		 * Mail
		 */
		public static final PumaAttribute MAIL = new PumaAttribute("mail");
		/**
		 * Mail 2
		 */
		public static final PumaAttribute MAIL_2 = new PumaAttribute("mail2");
		/**
		 * Tel&eacute;fono celular
		 */
		public static final PumaAttribute MOBILE = new PumaAttribute("mobile");
		/**
		 * Contrase&ntilde;a
		 */
		public static final PumaAttribute PASSWORD = new PumaAttribute("password");
		/**
		 * Nombres
		 */
		public static final PumaAttribute GIVEN_NAME = new PumaAttribute("givenName");
		/**
		 * Tipo de documento
		 */
		public static final PumaAttribute DOCUMENT_NUMBER = new PumaAttribute("documentNumber");
		/**
		 * Apellido materno
		 */
		public static final PumaAttribute MATERNAL_NAME = new PumaAttribute("maternalName");
		/**
		 * Tel&eacute;fono
		 */
		public static final PumaAttribute TELEPHONE_NUMBER = new PumaAttribute("telephoneNumber");
		
		private String value;

		/**
		 * @param value
		 */
		private PumaAttribute(String value)
		{
			this.value = value;
		}

		/**
		 * @return
		 */
		public String getValue()
		{
			return value;
		}
	}
}